#include <Arduino.h>

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include "receive.h"


void receive_init(){
  Mirf.cePin = 49;
  Mirf.csnPin = 48;
  Mirf.channel = 90;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();

  /*
   * Configure reciving address.
   */

  Mirf.setRADDR((byte *)"serv");

  /*
   * Set the payload length to sizeof(unsigned long) the
   * return type of millis().
   *
   * NB: payload on client and server must be the same.
   */

  Mirf.payload = sizeof(struct temp_msg_struct_ss);

  /*
   * Write channel and payload config then power up reciver.
   */

  /*
   * To change channel:
   * 
   * Mirf.channel = 10;
   *
   * NB: Make sure channel is legal in your area.
   */

  Mirf.config();
}

struct temp_msg_struct  get_temp_mirf()
{ 
  static uint8_t buffer[sizeof(struct temp_msg_struct)];   //création d'une variable tampon pour stocker les struct
  struct temp_msg_struct    *temp_msg = (struct temp_msg_struct *) &buffer[0];

  Mirf.payload = sizeof(struct temp_msg_struct);
  if (Mirf.dataReady())
  {
    // Serial.println("dataready");
    Mirf.getData(buffer);

    byte checksum = temp_msg->num_sonde + temp_msg->temp;          
    if (temp_msg->parity != checksum)
    {

      temp_msg->num_sonde = 0;
      temp_msg->temp = 0;
    }
  }
  else
  { 
    temp_msg->num_sonde = 0;
    temp_msg->temp = 0;
  }

  return(*temp_msg);
}







